package com.example.administrator.zhongyichou.view;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.administrator.zhongyichou.Bean.ChoiceBean;
import com.example.administrator.zhongyichou.adapter.ChoiceDialogAdapter;
import com.example.administrator.zhongyichou.R;

import java.util.List;

/**
 * 第三版新加
 * Created by lichenyu on 2017/5/31.
 */

public class ChoiceDialog extends Dialog {

    private ChoiceDialogAdapter adapter;
    private Context context;
    private OnClik onClik;
    private List<ChoiceBean> bean;

    ListView listview;


    public ChoiceDialog(Context context) {
        this(context, R.style.dialog);
        this.context = context;
    }

    public ChoiceDialog(Context context, int themeResId) {
        super(context, themeResId);
        View view = LayoutInflater.from(context).inflate(R.layout.choice_dialog, null);
        listview = (ListView) view.findViewById(R.id.listview);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                for (ChoiceBean choiceBean : bean) {
                    choiceBean.isSelect = false;
                }
                bean.get(position).isSelect = true;
                if (onClik != null) {
                    onClik.onClik(position);
                }
                dismiss();
            }
        });
        setContentView(view);
    }

    public void setData(List<ChoiceBean> bean) {
        if (this.bean == null)
            this.bean = bean;
        if (adapter == null)
            adapter = new ChoiceDialogAdapter(context, bean);
        listview.setAdapter(adapter);
    }

    public void setOnClik(OnClik onClik) {
        this.onClik = onClik;
    }

    public interface OnClik {
        void onClik(int position);
    }
}
